package com.allen.study.domain.listener;

import com.allen.study.domain.entity.SysEmpRole;
import com.allen.study.domain.service.EmployeeInfoService;
import com.allen.study.domain.service.SysEmpRoleService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @ClassName: CustomExecutionListener
 * @Author: AllenSun
 * @Date: 2025/2/23 下午9:11
 */
@Slf4j
@Component
public class AssigneeExecutionListener implements ExecutionListener {

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private EmployeeInfoService employeeInfoService;

    @Resource
    private SysEmpRoleService sysEmpRoleService;


    @Override
    public void notify(DelegateExecution execution) {
        log.info("开始动态设置审批人");
        log.info("节点：name :{}, id: {} 监听, 开始，监听类型 -> {}", execution.getCurrentActivityName(), execution.getId(), execution.getEventName());

        // 获取全部用户角色数据

        List<SysEmpRole> allSysEmpRole = sysEmpRoleService.queryAll().getData();
        Map<String, List<String>> roleKey2EmpidsMap =
                Optional.ofNullable(allSysEmpRole).orElse(Collections.emptyList()).stream().collect(Collectors.groupingBy(SysEmpRole::getRoleKey, Collectors.mapping(SysEmpRole::getEmployeeId, Collectors.toList())));
        execution.setVariables(roleKey2EmpidsMap);

        // String role = "";
        // if(ObjectUtil.equals(execution.getCurrentActivityName(),"发起审批")){
        //     role = "zuzhang";
        // } else if(ObjectUtil.equals(execution.getCurrentActivityName(),"组长审批网关")){
        //     role = "jingli";
        // } else if(ObjectUtil.equals(execution.getCurrentActivityName(),"项目经理审批网关")){
        //     role = "zhuguan";
        // }
        // // String role = (String) execution.getVariable("role");
        // log.info("获取角色类型：{}",role);
        // // if(ObjectUtil.isEmpty(role)){
        // //     role = "zuzhang";
        // // }
        // List<EmployeeInfo> employeeInfos = employeeInfoService.queryEmpsByRoleKey(role).getData();
        // List<String> previousApprover = CamundaUtils.getPreviousApprover(execution.getProcessInstanceId());
        // log.info("上一个节点的审批人：{}",previousApprover);
        //
        // List<String> assigneeList =
        //         Optional.ofNullable(employeeInfos).orElse(new ArrayList<>()).stream().filter(it->!previousApprover.contains(it.getEmployeeId())).map(EmployeeInfo::getEmployeeId).collect(Collectors.toList());
        // //设置审批人
        // log.info("角色{}设置审批人：{}",role,assigneeList.toString());
        // execution.setVariable("assigneeList", assigneeList);
        log.info("节点：name :{}, id: {} 监听 结束，监听类型 -> {}", execution.getCurrentActivityName(), execution.getId(), execution.getEventName());
    }
}
